#include <iostream>
#include <memory>
#include "Common.hpp"
#include "Socket.hpp"
#include "Log.hpp"
#include "Protocol.hpp"
using namespace LogModule;
using namespace SocketModule;
inline void GetDatafromstdin(int *x, int *y, char *oper)
{
    cout << "Please Enter x:";
    cin >> *x;
    cout << "Please Enter oper:";
    cin >> *oper;
    cout << "Please Enter y:";
    cin >> *y;
}
int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        LOG(Level::FATAL) << "Usage serever's ip and port";
        exit(USAGE_ERRO);
    }
    shared_ptr<Socket> client = make_shared<TcpSocket>();
    unique_ptr<Protocol> ptl = make_unique<Protocol>();
    client->SocketOrDie();
    client->Connect(argv[1], stoi(argv[2]));
    string buffer;

    while (true)
    {

        int x, y;
        char oper;
        GetDatafromstdin(&x, &y, &oper);
        string send_str = ptl->BuildRequestString(x, y, oper);
        client->Send(send_str);
        Response rsp;
        if (!ptl->GetResponse(client, &buffer, &rsp))
            break;
        rsp.ShowResult();
    }
    return 0;
}